//
//  ViewController.m
//  14-HomeWork
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "LableAndImageCollectionViewCell.h"
#import "LabelAndButtonCollectionReusableView.h"
#import "HeaderCollectionReusableView.h"

#define kLabelAndImageViewCollectionViewCell @"labelAndImageViewReuse"
#define kLableAndButtonReusableView @"labelAndButtonReusable"
#define kHaderCollectionReusableView @"haderCollectionReusableView"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:1.00 green:0.02 blue:0.29 alpha:1.00];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"攻略",@"单品"]];
    segment.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = segment;
    
    
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((CGRectGetWidth(self.view.frame) - 50) / 2, (CGRectGetHeight(self.view.frame) - 50) / 3);
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 0;
    flow.headerReferenceSize = CGSizeMake(0, 80);
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
  //contentInset
    _collectionView.contentInset = UIEdgeInsetsMake(80, 0, 0, 0);
    UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeSystem];
    sortButton.frame = CGRectMake(0, -80, self.view.bounds.size.width, 50);
    [sortButton setTitle:@"快选一个七夕礼物， 送给心爱的TA吧" forState:UIControlStateNormal];
    [sortButton setTitleColor:[UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1.00] forState:UIControlStateNormal];
    [_collectionView addSubview:sortButton];
    
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[LableAndImageCollectionViewCell class] forCellWithReuseIdentifier:kLabelAndImageViewCollectionViewCell];
    [_collectionView registerClass:[LabelAndButtonCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kLableAndButtonReusableView];
    
    [_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHaderCollectionReusableView];
}

//header 的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return CGSizeMake(0, 400);
    } else {
       return CGSizeMake(0, 80);
    }
}


//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
//row
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 6;
}
//cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LableAndImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLabelAndImageViewCollectionViewCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor yellowColor];
    return cell;
}
//头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HeaderCollectionReusableView * haderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHaderCollectionReusableView forIndexPath:indexPath];
        haderView.backgroundColor = [UIColor whiteColor];
        return haderView;
    }
    LabelAndButtonCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kLableAndButtonReusableView forIndexPath:indexPath];
    return reusableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
